package com.ss.android.article.base.ui;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.feed.c.FeedDownloadAppAd;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.browser.R;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.download.DownloadAdapterProxy;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.util.AppUtil;

import java.lang.ref.WeakReference;

import org.json.JSONObject;

public class LargeVideoGrayAreaLayout extends RelativeLayout {
    final static class a implements DownloadInfoChangeListener {
        public final LargeVideoGrayAreaLayout b;
        long a;

        a(LargeVideoGrayAreaLayout arg1) {
            this.b = arg1;

        }

        public final void setDownloadId(long id) {
            this.a = id;
        }

        public final void onDownloadProgressChanged(DownloadShortInfo arg10, int arg11, long arg12, long arg14, long arg16) {
            this.b.post(new w(this, arg10, arg11, arg12, arg14));
        }
    }

    public static final class b extends AsyncTask {
        private final LargeVideoGrayAreaLayout a;

        public b(LargeVideoGrayAreaLayout arg1) {
            this.a = arg1;

        }

        protected final Object doInBackground(Object[] arg7) {
            Object v0_1;
            if (arg7 != null) {
                if (arg7.length > 0 && (TextUtils.isEmpty((CharSequence) arg7[0]))) {
//                    goto label_10;
                    v0_1 = null;
                } else if (this.a.mContext == null) {
//                    goto label_10;
                    v0_1 = null;
                } else {
                    Object v1 = arg7[0];
                    v0_1 = DownloadAdapterProxy.inst().queryDownloadInfo(this.a.mContext, ((String) v1));
                    if (v0_1 == null) {
                        return null;
                    }
                    Logger.d("AdButtonLayout queryDownloadInfo", "result.id = " + ((DownloadShortInfo) v0_1).id + " url = " + (((String) v1)) + " fileName = " + ((DownloadShortInfo) v0_1).fileName);
                }
            } else {
                label_10:
                v0_1 = null;
            }

            return v0_1;
        }

        protected final void onPostExecute(Object arg11) {
            Resources v1;
            TextView v2;
            String v0_3 = null;
            CharSequence v7 = null;
            int v0 = R.string.ke;
            super.onPostExecute(arg11);
            if (!this.isCancelled()) {
                this.a.a();
                boolean v3 = AppUtil.a(this.a.mContext, this.a.mFeedDownloadAppAd.version, this.a.mFeedDownloadAppAd.pkg);
                try {
                    if ((AppData.getInstance().allowInsideDownloadManager()) && arg11 != null && ((DownloadShortInfo) arg11).id > -1 && !DownloadAdapterProxy.inst().isDownloadSuccessAndFileNotExist(this.a.mContext, ((DownloadShortInfo) arg11)) && !v3) {
                        Logger.d("AdButtonLayout QueryDownloadInfoTask", "result.id = " + ((DownloadShortInfo) arg11).id + " fileName = " + ((DownloadShortInfo) arg11).fileName);
                        DownloadAdapterProxy.inst().registerDownloadListener(this.a.mContext, Long.valueOf(((DownloadShortInfo) arg11).id), this.a.h, String.valueOf(this.a.mFeedDownloadAppAd.id), 7, this.a.mFeedDownloadAppAd.logExtra);
                        this.a.f = ((DownloadShortInfo) arg11);
                        this.a.a(false);
                        switch (((DownloadShortInfo) arg11).b) {
                            case 1:
                            case 2: {
                                this.a.a(true);
                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.kf);
                                break;
                            }
                            case 4: {
                                this.a.a(true);
                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.ki);
                                break;
                            }
                            case 8: {
                                this.a.a(true);
                                v0_3 = ToolUtils.d(this.a.mContext, ((DownloadShortInfo) arg11).fileName) ? this.a.getResources().getString(com.ss.android.article.browser.R.string.ke) : this.a.getResources().getString(com.ss.android.article.browser.R.string.k9);
                                break;
                            }
                            case 16: {
                                v0_3 = this.a.getResources().getString(com.ss.android.article.browser.R.string.kh);
                                break;
                            }
                            default: {
                                v0_3 = null;
                                break;
                            }
                        }
                    } else {
                        if (this.a.f != null) {
                            DownloadAdapterProxy.inst().unregisterDownloadListener(this.a.mContext, Long.valueOf(this.a.f.id), this.a.h);
                        }

                        this.a.f = null;
                        if (ToolUtils.isAppInstalled(this.a.mContext, this.a.mFeedDownloadAppAd.pkg)) {
                            v2 = this.a.b;
                            v1 = this.a.getResources();
                        } else {
                            v2 = this.a.b;
                            v1 = this.a.getResources();
                            v0 = v3 ? R.string.kj : R.string.ka;
                        }

                        UIUtils.setText(v2, v1.getString(v0));
                        this.a.a(false);
                        return;
                    }
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }

                try {
                    UIUtils.setText(this.a.b, v0_3);
                    if (((DownloadShortInfo) arg11).c > 0 && (UIUtils.isVisible(this.a.a))) {
                        this.a.a.setProgress(((int) (((DownloadShortInfo) arg11).d * 100 / ((DownloadShortInfo) arg11).c)));
                        return;
                    }

                    this.a.a.setProgress(0);
                    return;
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }

    protected ProgressBar a;
    protected TextView b;
    public FeedDownloadAppAd mFeedDownloadAppAd;
    public Context mContext;
    public CellRef e;
    public DownloadShortInfo f;
    public com.ss.android.article.base.ui.LargeVideoGrayAreaLayout.b g;
    public final a h;
    private boolean i;
    private EllipsisTextView j;
    private RelativeLayout k;
    private WeakReference mContextRef;
    private View.OnClickListener m;

    public LargeVideoGrayAreaLayout(Context arg2) {
        super(arg2);
        this.i = false;
        this.b = null;
        this.h = new a(this);
        this.m = new v(this);
        this.a(arg2);
    }

    public LargeVideoGrayAreaLayout(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.i = false;
        this.b = null;
        this.h = new a(this);
        this.m = new v(this);
        this.a(arg2);
    }

    public LargeVideoGrayAreaLayout(Context arg2, AttributeSet arg3, int arg4) {
        super(arg2, arg3, arg4);
        this.i = false;
        this.b = null;
        this.h = new a(this);
        this.m = new v(this);
        this.a(arg2);
    }

    public final void a() {
        if (this.e != null) {
            if (!android.support.design.a.isTextEmpty(this.e.subTitle) && !android.support.design.a.isTextEmpty(this.e.subTitle.trim())) {
                UIUtils.setText(this.j, this.e.subTitle);
            } else {
                UIUtils.setText(this.j, this.mFeedDownloadAppAd.source);
            }
        } else {
            UIUtils.setText(this.j, this.mFeedDownloadAppAd.source);
        }

        label_15:
        UIUtils.setText(this.b, this.mFeedDownloadAppAd.buttonText);
        UIUtils.setVisibility(this.a, 8);
        this.b.setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi5));
        UIUtils.setBackgroundResource(this.k, R.drawable.ad_action_btn_begin_bg);
    }

    private void a(Context context) {
        this.mContext = context;
        this.mContextRef = new WeakReference(this.mContext);
        LargeVideoGrayAreaLayout.inflate(context, R.layout.feed_large_video_below_gray, ((ViewGroup) this));
        this.a = this.findViewById(com.ss.android.article.browser.R.id.btn_ad_progress_bar);
        this.b = this.findViewById(com.ss.android.article.browser.R.id.btn_ad_tv);
        this.b.setOnClickListener(this.m);
        this.j = this.findViewById(com.ss.android.article.browser.R.id.large_image_text);
        this.k = this.findViewById(com.ss.android.article.browser.R.id.ad_progress_layout);
        AppData.getInstance();
        this.i = AppData.isNightMode();
    }

    protected final void a(boolean arg4) {
        AppData.getInstance();
        AppData.isNightMode();
        if (arg4) {
            UIUtils.setVisibility(this.a, 0);
            this.b.setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi8));
            UIUtils.setBackgroundResource(this.k, R.color.transparent);
        } else {
            UIUtils.setVisibility(this.a, 8);
            this.b.setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi5));
            UIUtils.setBackgroundResource(this.k, R.drawable.ad_action_btn_begin_bg);
        }
    }

    public final void b(boolean arg4) {
        if (this.i != arg4) {
            this.i = arg4;
            UIUtils.setBackgroundResource(((View) this), R.color.ssxinmian3);
            if (this.b != null) {
                this.b.setTextColor(ContextCompat.getColor(this.mContext, R.color.ssxinzi8));
            }

            if (this.a == null) {
                return;
            }

            this.a.setProgressDrawable(this.getResources().getDrawable(com.ss.android.article.browser.R.drawable.video_ad_button_progress_bg));
            this.a.getProgressDrawable().setBounds(this.a.getProgressDrawable().getBounds());
        }
    }

    final void b() {
        int v5 = 7;
        DownloadShortInfo v3 = null;
        int v2 = 2;
        if (this.mFeedDownloadAppAd != null) {
            if ("app".equals(this.mFeedDownloadAppAd.type)) {
                if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
                    UIUtils.a(this.mContext);
                    return;
                }

                this.mFeedDownloadAppAd.J = System.currentTimeMillis();
                if (AppData.getInstance().allowInsideDownloadManager()) {
                    this.mFeedDownloadAppAd.a(this.c(), v2, this.f, this.h, v5);
                    return;
                }

                this.mFeedDownloadAppAd.a(this.c(), v2, v3, null, v5);
                return;
            }

            if ("action".equals(this.mFeedDownloadAppAd.type)) {
                JSONObject v10 = new JSONObject();
                try {
                    if (android.support.design.a.isTextEmpty(this.mFeedDownloadAppAd.logExtra)) {
//                        goto label_49;
                    } else {
                        v10.put("log_extra", this.mFeedDownloadAppAd.logExtra);
                    }
                } catch (Exception v0) {
                }

                label_49:
                if (this.mFeedDownloadAppAd.actionType != 1) {
                    return;
                }

                if (android.support.design.a.isTextEmpty(this.mFeedDownloadAppAd.phoneNumber)) {
                    return;
                }

                MobAdClickCombiner.onAdEvent(this.mContext, this.getEventName(), "click_call", this.mFeedDownloadAppAd.id, ((long) this.mFeedDownloadAppAd.actionType), v10, v2);
                ToolUtils.e(this.c(), this.mFeedDownloadAppAd.phoneNumber);
                return;
            }

            if ("web".equals(this.mFeedDownloadAppAd.type)) {
                try {
                    JSONObject v12 = new JSONObject();
                    if (!android.support.design.a.isTextEmpty(this.mFeedDownloadAppAd.logExtra)) {
                        v12.put("log_extra", this.mFeedDownloadAppAd.logExtra);
                    }

                    MobAdClickCombiner.onAdEvent(this.mContext, this.getEventName(), "click", this.mFeedDownloadAppAd.id, 0, v12, 2);
                    MobAdClickCombiner.onAdEvent(this.mContext, this.getEventName(), "ad_click", this.mFeedDownloadAppAd.id, 0, v12, 2);
                } catch (Exception v0) {
                }

                AdsAppItem.a(this.c(), this.mFeedDownloadAppAd.openUrl, this.mFeedDownloadAppAd.webUrl, this.mFeedDownloadAppAd.webTitle, this.mFeedDownloadAppAd.orientation, new AdsAppItem.b(this.mContext, "embeded_ad", null, this.mFeedDownloadAppAd.id, this.mFeedDownloadAppAd.logExtra));
            }
        }
    }

    private Context c() {
        Context v0_1;
        if (this.mContextRef == null || this.mContextRef.get() == null) {
            v0_1 = this.mContext;
        } else {
            v0_1 = (Context) this.mContextRef.get();
        }

        return v0_1;
    }

    protected String getEventName() {
        String v0;
        if ("app".equals(this.mFeedDownloadAppAd.type)) {
            v0 = "feed_download_ad";
        } else if ("action".equals(this.mFeedDownloadAppAd.type)) {
            v0 = "feed_call";
        } else if ("web".equals(this.mFeedDownloadAppAd.type)) {
            v0 = "embeded_ad";
        } else {
            v0 = "";
        }

        return v0;
    }
}

